<template>
  <div>
    <el-dialog
      v-if="this.show"
      v-loading="id && $apollo.queries.emailTemplate.loading"
      :title="id ? '编辑' : '添加模板'"
      :close-on-click-modal="false"
      custom-class="!mt-[5vh]"
      width="70vw"
      :visible="this.show"
      append-to-body
      @open="getInit"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :model="form"
        class="mt-0 mb-5 mx-5"
        label-width="70px"
      >
        <el-form-item :label="$t('email.subject')">
          <el-input v-model="form.title" show-word-limit></el-input>
        </el-form-item>

        <el-dropdown
          class="ml-17 mb-3"
          size="mini"
          split-button
          type="success"
          @command="(cmd) => editorInput(cmd)"
        >
          插入变量
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="[SN]">[SN] : 发件昵称</el-dropdown-item>
            <el-dropdown-item command="[SA]">[SA] : 发件地址</el-dropdown-item>
            <el-dropdown-item command="[RN]">[RN] : 收件昵称</el-dropdown-item>
            <el-dropdown-item command="[RA]">[RA] : 收件地址</el-dropdown-item>
            <!-- <el-dropdown-item command="[####]"
            >[####] : 随机日期</el-dropdown-item
          >
          <el-dropdown-item command="[****]"
            >[****] : 随机字符</el-dropdown-item
          >
          <el-dropdown-item command="[$$$$]"
            >[$$$$] : 随机数字</el-dropdown-item
          >
          <el-dropdown-item command="[%%%%]"
            >[%%%%] : 随机符号</el-dropdown-item
          > -->
          </el-dropdown-menu>
        </el-dropdown>
        <el-form-item :label="$t('email.content')">
          <tinymce-page
            ref="tinymce"
            v-model="form.templateContent"
            :height="500"
          ></tinymce-page>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" @click="handleSaveTemplate">{{
          $t('permission.confirm')
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { postAction } from '@/api/common'
import gql from 'graphql-tag'

import TinymcePage from '../../Tinymce/index.vue'
export default {
  name: 'AddTemplate',
  props: {
    id: {
      type: [Number, String],
      default: '',
    },
    show: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    TinymcePage,
  },
  apollo: {
    emailTemplate: {
      query: gql`
        query GetEmailTemplateById($id: Int!) {
          emailTemplate: emailTemplateById(id: $id) {
            id
            title
            templateContent
            type
            attribute
          }
        }
      `,
      variables() {
        return { id: this.id }
      },
      skip() {
        return !this.id
      },
      result(res, key) {
        this.form = { ...this.form, ...res.data.emailTemplate }
      },
    },
  },
  created() {
    this.getInit()
  },
  data: function () {
    return {
      form: {
        title: '',
        templateContent: '',
        type: 1,
        attribute: 1,
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$t('common.enter', [this.$t('customer.groupName')]),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    getInit() {},
    handleClose() {
      this.resetForm()
      this.$emit('update:show', false)
      this.$emit('update:id', '')
      console.log(this.form, this.$refs.form)
    },
    resetForm() {
      this.form = {
        title: '',
        templateContent: '',
        type: 1,
        attribute: 1,
      }
    },
    handleSaveTemplate() {
      if (!this.form.title) return this.$message.warning('请填写标题')
      let data = this.form
      let url
      if (this.id) {
        url = 'EmailTemplate/Edit'
        data.id = this.id
      } else {
        url = 'EmailTemplate/Create'
      }
      postAction(this.form, url).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: this.$t('email.saveEDMTemplateSuccessfully'),
            type: 'success',
          })

          this.handleClose()
          this.$emit('closeAction', res.data.id)
        } else {
          this.$message({
            message: this.$t('email.saveEDMtemplateFailed'),
            type: 'warning',
          })
        }
      })
    },

    editorInput(str) {
      this.$refs.tinymce.insertContent(str)
    },
  },
}
</script>

<style scoped></style>
